package com.qianclass.talklive.app.im;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class LogFile {
	private File __log = null;
	public LogFile(){
		CreateLogFile();
	}
 	public boolean CreateLogFile()
	{
		File directory = new File("log"); 
		String path = directory.getAbsolutePath(); 
		
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		String curDate = df.format(new Date());
		
		String logFile = path + "\\" + curDate + ".txt";		
		__log = new File(logFile);
		
		System.out.print("create log file: " + logFile + "\n");
		
		if(!__log.getParentFile().exists())
		{
			__log.getParentFile().mkdirs();
		}
		if (!__log.exists()) {
			try {
				__log.createNewFile();
			} catch (IOException e) {
				return false;
			}
		} else {
			__log.delete();
		}
		return true;
	}
	
	public boolean WriteLine(String line)
	{
		try 
		{			
			FileWriter writer = new FileWriter(__log, true);
			SimpleDateFormat df = new SimpleDateFormat("HH:mm:ss");
			String curTime = df.format(new Date());
			writer.write(curTime);
			writer.write(" ");				
			writer.write(line);
			writer.write("\r\n");
			writer.close();
		}
		catch (IOException e) 
		{
			return false;
		}
		return true;
	}
}
